<template>
  <el-dialog :title="isEdit?'编辑库房设置':'新增库房设置'"
             :visible.sync="dialogForm"
             width="720px">
    <el-form :inline="true"
             ref="form"
             :model="form"
             :rules="rules"
             class="add-form"
             label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="库房代码"
                        prop="roomCode">
            <el-input v-model="form.roomCode"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库房名称"
                        prop="roomName">
            <el-input v-model="form.roomName"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="库房类型"
                        prop="roomType">
            <el-select v-model="form.roomType"
                       placeholder="请选择">
              <el-option v-for="[key, val] in _dictRoomType"
                         :key="key"
                         :label="val"
                         :value="key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属单位"
                        prop="orgId">
            <auto-search-org v-model="form.orgId"
                             @select="v=>{form.orgId=v;form.deptId='';form.deptName=''}"
                             @selectName="v=>form.orgName=v"
                             :disabled="true"
                             :name="form.orgName"></auto-search-org>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="所属仓库"
                        prop="warehouseId">
            <el-select v-model="form.warehouseId"
                       placeholder="请选择">
              <el-option v-for="item in warehouseList"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最大跨数"
                        prop="maxSpan">
            <el-input v-model="form.maxSpan"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="最大间数"
                        prop="maxAula">
            <el-input v-model="form.maxAula"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注"
                        prop="remark">
            <el-input type="textarea"
                      :rows="3"
                      style="width: 560px"
                      placeholder="请输入内容"
                      :maxLength="$maxLength"
                      v-model="form.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer"
         class="dialog-footer">
      <el-button @click="dialogForm = false">取 消</el-button>
      <el-button type="primary"
                 @click="handleSubmit"
                 :loading="$store.getters.loadingBtn">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse'//所属仓库来源于管辖仓库
import { addStorageSet, updateStorageSet } from '@/api/storageSet/storageSet'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchDic from '@/components/autocomplete/autoSearchDic'
// import autoSearchTer from '@/components/autocomplete/autoSearchTer'
// import searchDept from '@/components/autocomplete/autoSearchDept'
import { maxLength, sortLength, twoLength } from '@/utils/rules'
import {
  PUBLIC_LIMIT,
  PUBLIC_INT_NULL_ZERO,
  PUBLIC_NUM_CAP
} from '@/utils/Reg'
export default {
  name: 'storageSetAddForm',
  components: { autoSearchOrg, autoSearchDic },
  data() {
    return {
      form: this.clearForm(),
      rules: {
        roomCode: [
          { required: true, validator: PUBLIC_NUM_CAP, trigger: 'blur' }, //message: '请输入一位大写字母+2位数字，例：A01',
          sortLength
        ],
        roomName: [
          { required: true, validator: PUBLIC_LIMIT, trigger: 'blur' },
          sortLength
        ],
        roomType: [
          { required: true, message: '请选择库房类型', trigger: 'blur' }],
        orgId: [{ required: true, message: '请选择单位', trigger: 'change' }],
        warehouseId: [
          { required: true, message: '请选择仓库类型', trigger: 'blur' }],
        maxSpan: [{ required: true, validator: PUBLIC_INT_NULL_ZERO, message: '最多输入2位数字',trigger: 'blur' }],
        maxAula: [{ required: true, validator: PUBLIC_INT_NULL_ZERO, message: '最多输入2位数字',trigger: 'blur' }],
        remark: [maxLength]
      },
      dialogForm: false,
      isEdit: false,
      warehouseList: null
    }
  },
  mounted() {
    getWarehouseList().then(res => {
      this.warehouseList = res.data.aaData
    })
  },
  methods: {
    clearForm() {
      return {
        roomCode: '',
        roomName: '',
        roomType: '',
        roomTypeDesc:'',
        warehouseId: '',
        maxSpan: 0,
        maxAula: 0,
        remark: '',
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name
      }
    },
    // 保存
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.handleToEdit() : this.handleToAdd()
        } else {
          this.$message({
            message: '请检查表单',
            type: 'error'
          })
        }
      })
    },
    // 新增
    handleToAdd() {
      addStorageSet(this.form).then(res => {
        this.$message({ message: '新增成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    // 编辑
    handleToEdit() {
      updateStorageSet(this.form.id, this.form).then(res => {
        this.$message({ message: '编辑成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    handleCllx(v) {
      if (v === 'JOB_CLLX_HC') {
        this.form.teu = 0
      }
      this.form.typeCode = v
    }
  },
  watch: {
    dialogForm(bool) {
      if (!bool) {
        this.$refs.form.resetFields()
        this.isEdit = false
        this.form = this.clearForm()
        this.$emit('clear')
      }
    }
  }
}
</script>

<style scoped>
</style>
